# 分组匹配
"""
group：()  两种：（number 、name）
    分组：() -->result.group(1) 获取组中的匹配内容   名字匹配
        分组的时候可以结合 |
            phone = "010-12345667"
            match_phone = re.match(r"(\d{3}|\d{4})-(\d{8})$", phone)
            print(match_phone)  # <re.Match object; span=(0, 12), match='010-12345667'>
    不需要引用分组的内容：引用匹配
        msg = "<html><h1>abc</h1></html>"
        result_msg = re.match(r"<([0-9a-zA-Z]+)><([0-9a-zA-Z]+)>(.+)</\2></\1>$", msg)
        print(result_msg)  # <re.Match object; span=(0, 25), match='<html><h1>abc</h1></html>'>
        print(result_msg.group(1))
    引用分组匹配的内容：
        1、number （标签少） \number  引用第number组的内容
            msg = "<html><h1>abc</h1></html>"
            result_msg = re.match(r"<([0-9a-zA-Z]+)><([0-9a-zA-Z]+)>(.+)</\2></\1>$", msg)
        2、?P<名字>（标签多）
            s = "<html><body><h1>hello world</h1></body></html>"
            s_match = re.match(r"<(?P<name1>\w+)><(?P<name2>\w+)><(?P<name3>\w+)>(.+)</(?P=name3)></(?P=name2)></(?P=name1)>", s)
            print(s_match)  # <re.Match object; span=(0, 46), match='<html><body><h1>hello world</h1></body></html>'>
            print(s_match.group(1))
    re模块：
        match:从头匹配到尾，只匹配一次
        search：找到第一个就停止查找，并返回：只匹配一次
        findall：查找所有
        sub（正则表达式、需要替换的新内容或者函数、原字符串）:替换
        split：字符串中搜索如果遇到 空格 就分割，将分割的类容都保存到列表中
            re_split = re.split(r" ", "java:80 python:70")  # 空格
            print(re_split)  # ['java:80', 'python:70']

            split(pattern,str)-->[]
"""
import re

# 匹配数字0-100

n = "50"

re_match = re.match(r"[1-9]?\d", n)
print(re_match)  # <re.Match object; span=(0, 2), match='50'>

nu = "09"
re_match = re.match(r"[1-9]?\d", nu)
print(re_match)  # <re.Match object; span=(0, 1), match='0'>   有问题

num = "100"
re_match = re.match(r"[1-9]?\d", num)  # <re.Match object; span=(0, 2), match='10'>
print(re_match)

re_match = re.match(r"[1-9]+\d*", num)  # <re.Match object; span=(0, 2), match='10'>
print(re_match)  # <re.Match object; span=(0, 3), match='100'>

# 改进版
new_num = "100"
re_match = re.match(r"[1-9]?\d?$|100$", new_num)  # <re.Match object; span=(0, 3), match='100'>
print(re_match)  # <re.Match object; span=(0, 2), match='10'>

# 验证邮箱163 qq  126

email = "123@qq.com"

match_email = re.match(r"\w+@(qq|163|126).com$", email)  # (qq|163|126)  小括号表示整体返回的或者

print(match_email)  # <re.Match object; span=(0, 10), match='123@qq.com'>

# 邮件名称必须大于5位，小于20位
email = "1006779691@163.com"

match_email = re.match(r"\w{5,20}@(qq|163|126)\.com$", email)  # (qq|163|126)  小括号表示整体返回的或者

print(match_email)  # <re.Match object; span=(0, 18), match='1006779691@163.com'>

print("正则分组操作".center(20, "*"))
# group

# a2b h6k

msg = 'bckshsaa cinlwn&nel*223jluin8819jhunk21nlini'

result = re.search(r"[a-z]\d+[a-z]", msg)  # search只要找到就停下了
# print(result)
print(result.group())  # ['n8819j', 'k21n']

result = re.findall(r"[a-z]\d+[a-z]", msg)  # findall查找字符串中所有
print(result)  # ['n8819j', 'k21n']

result = re.match(r"[a-z]\d+[a-z]", msg)  # search只要找到就停下了
print(result)

# print(result.group())  # ['n8819j', 'k21n']


# qq号码验证5-11位；0不能开头

qq = "1234"
qq = "100799979"
result = re.match(r"^[1-9]\d{4,10}$", qq)  # <re.Match object; span=(0, 9), match='100799979'>
print(result)

# 分组起名：用起名的方式处理  (?P<名字>正则) 引用(?P=名字)
s = "<html><body><h1>hello world</h1></body></html>"

s_match = re.match(r"<(?P<name1>\w+)><(?P<name2>\w+)><(?P<name3>\w+)>(.+)</(?P=name3)></(?P=name2)></(?P=name1)>", s)

print(s_match)  # <re.Match object; span=(0, 46), match='<html><body><h1>hello world</h1></body></html>'>
print(s_match.group(1))
print(s_match.group(2))
print(s_match.group(3))
print(s_match.group(4))
# 使用数字  分组
msg = "<html><h1>abc</h1></html>"
result_msg = re.match(r"<([0-9a-zA-Z]+)><([0-9a-zA-Z]+)>(.+)</\2></\1>$", msg)
print(result_msg)  # <re.Match object; span=(0, 25), match='<html><h1>abc</h1></html>'>
print(result_msg.group(1))
print(result_msg.group(2))
print(result_msg.group(3))

# sub
re_sub = re.sub(r"\d+", "100", "java:80 python:70")
print(re_sub)  # java:100 python:100


# 函数替换
def func(temp):
    nm = temp.group()
    new_nm = int(nm) + 10
    return str(new_nm)


res_sub = re.sub(r"\d+", func, "java:80 python:70")
print(res_sub)  # java:90 python:80

re_split = re.split(r"[,: ]", res_sub)  # 逗号、冒号、空格
print(re_split)  # ['java', '90', 'python', '80']

re_split = re.split(r" ", "java:80 python:70")  # 空格
print(re_split)  # ['java:80', 'python:70']

# 不是以 4、7 结尾的收集号码 11位
phone = "18700191128"
match_phone = re.match(r"1\d{9}[0-35-68-9]$", phone)
print(match_phone)  # <re.Match object; span=(0, 11), match='18700191128'>
print(match_phone.group())  # 18700191128

# 爬虫：分组
phone = "010-12345667"
match_phone = re.match(r"(\d{3}|\d{4})-(\d{8})$", phone)
print(match_phone)  # <re.Match object; span=(0, 12), match='010-12345667'>

# 分别提取
# () 表示分组，groups(1)：表示提取第一组的内容，groups(2)表示第二组的内容
print(match_phone.group())  # 默认整体匹配:010-12345667
print(match_phone.group(1))  # 010
print(match_phone.group(2))  # 12345667

# 引用分组 \1
msg = "<html>abc</html>"
msg_match = re.match(r"<([0-9a-zA-Z]+)>(.+)</\1>$", msg)
print(msg_match)  # <re.Match object; span=(0, 16), match='<html>abc</html>'>
print(msg_match.groups())  # ('html', 'abc')
print(msg_match.group(2))  # abc
